package leesoft.intg.plugins;

import java.util.Map;

import leesoft.configuration.core.RuntimeContext;
import leesoft.configuration.interfaces.AfterPlugin;

public class AddIncreasingField extends AfterPlugin {

	@Override
	public void beforeExecute(){
		int step=10;
		if(parameters.get("step")!=null){
			step=Integer.parseInt((String)parameters.get("step"));
		}
		session.put(this.getIdentifier(), step);
	}
	public void execute(Map rsMap) {
		int step=10;
		if(parameters.get("step")!=null){
			step=Integer.parseInt((String)parameters.get("step"));
		}
		String fieldname=(String)parameters.get("fieldName");
		int currentIncreasingNumber=session.getInt(this.getIdentifier());
		
		String prefix="0000";
		if(parameters.get("prefix")!=null){
			prefix=(String)parameters.get("prefix");
		}

		String type="string";
		if(parameters.get("type")!=null){
			type=(String)parameters.get("type");
		}
		String value="";
		if(type.equals("string")){
			value=prefix+currentIncreasingNumber;
			value=value.substring(value.length()-prefix.length()-1, value.length()-1);	
		}else if(type.equals("int")){
			value=""+currentIncreasingNumber;
		}
		rsMap.put(fieldname, value);
		currentIncreasingNumber=currentIncreasingNumber+step;
		session.put(this.getIdentifier(), currentIncreasingNumber);
	}

}
